package com.dbxt.clerk.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.dbxt.clerk.service.ClerkService;
import com.dbxt.entity.Clerk;
import com.dbxt.entity.ClerkRecord;
import com.dbxt.entity.RecordTeacher;
import com.dbxt.entity.Student;
import com.dbxt.teacher.service.TeacherService;
/**
 * 
 * @author Naive
 * @date 2020年5月14日
 */
@Controller
@RequestMapping("/clerk")
public class CleckController {
	@Resource
	private ClerkService clerkService;
	@Resource
	private TeacherService impl;
	/**
	 *	添加答辩记录
	 * @param sid 学生id
	 * @param context 记录内容
	 * @param cid 书记员id
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/addContext" ,method = RequestMethod.POST)
	public Map<String , Object> addContext(Integer sid ,String context ,Integer cid ,HttpServletRequest request){
		Map<String, Object> map = new HashMap<>();
		ClerkRecord clerkRecord = new ClerkRecord();
		Clerk clerk = new Clerk();
		clerk.setCid(cid);
		clerkRecord.setClerk(clerk);
		clerkRecord.setContext(context);
		if (clerkService.addRecord(clerkRecord, sid)) {
			map.put("status", 0);
		}else{
			map.put("status", 1);
		}
		return map;
	}
	@ResponseBody
	@RequestMapping(value="/updateContext",method=RequestMethod.POST)
	public JSONObject updateContext(Integer sid,Integer cid,String context,HttpSession session,Integer crid){
		int status=-1;
		JSONObject json = new JSONObject();
		Student selectRecordByStudent = impl.selectRecordByStudent(sid);
		Clerk selectClerk = impl.selectClerk(cid);
		ClerkRecord selectCrid = clerkService.selectCrid(crid);
		if (selectCrid.getClerk()==null) {
			selectCrid.setClerk(selectClerk);
			boolean updateCid = clerkService.updateCid(selectCrid);
			if (updateCid) {
				ClerkRecord selectClerkRecordByClerk = impl.selectClerkRecordByClerk(cid,selectRecordByStudent.getRecord().getRid());		
				selectClerkRecordByClerk.setContext(context);
					boolean updateContext = clerkService.updateContext(selectClerkRecordByClerk);
					if (updateContext) {
						status=0;
						json.put("clerkRecordUpdate", selectClerkRecordByClerk);
						json.put("message", "修改成功");				
					}
					else {
						status=1;
						json.put("message", "修改失败");
					}
			}		
		}
		else {
			ClerkRecord selectClerkRecordByClerk = impl.selectClerkRecordByClerk(cid,selectRecordByStudent.getRecord().getRid());		
			selectClerkRecordByClerk.setContext(context);
				boolean updateContext = clerkService.updateContext(selectClerkRecordByClerk);
				if (updateContext) {
					status=0;
					json.put("clerkRecordUpdate", selectClerkRecordByClerk);
					json.put("message", "修改成功");				
				}
				else {
					status=1;
					json.put("message", "修改失败");
				}
		}
		
		json.put("status", status);
		return json;
		
	}
	/**
	 * 	查看答辩记录
	 * @param sid 学生id
	 * @param session
	 * @param request
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/selectClerkRecord" ,method = RequestMethod.GET)
	public Map<String , Object> selectClerkRecord(Integer sid ,HttpSession session ,HttpServletRequest request){
		Map<String, Object> map = new HashMap<>();
		Clerk clerk = (Clerk)session.getAttribute("clerk");
		map.put("clerkContext", clerkService.selectClerkRecord(clerk.getCid(), sid));
		return map;
	}
		
}
